package com.tywj.lib.core.common.extension

import android.annotation.SuppressLint
import android.view.View
import com.jakewharton.rxbinding3.view.clicks
import com.tywj.lib.core.common.util.LogUtil
import java.util.concurrent.TimeUnit

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/7
 *     desc   : View扩展
 *     version: 1.0.0
 * </pre>
 */

// 点击时间间隔
private const val TIME_INTERVAL = 1000L

/**
 * View点击事件，限定时间之内只取第一个点击事件，防止重复点击
 */
@SuppressLint("CheckResult")
fun View.onClick(time: Long = TIME_INTERVAL, click: (View) -> Unit) {
    this.clicks()
        .throttleFirst(time, TimeUnit.MILLISECONDS)
        .subscribe({
            click(this)
        }, {
            LogUtil.printStackTrace(it)
        })
}